package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.app.mess.NewBehaviorDTO;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.constants.article.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 29143
 */
@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 点赞或取消点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        //参数校验
        ApUser user = AppThreadLocalUtils.getUser();
        //判断用户是否登录
        if (user==null) {
            log.error("当前没有登陆用户,不允许此操作");
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW);
        }
        //获取操作方式
        Short operation = dto.getOperation();
        if (operation.intValue() != 1 && operation.intValue() != 0) {
            log.error("点赞方式必须只能是0或者1");
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据当前登录用户id查询行为实体信息
        ApBehaviorEntry entry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), null);
        //构建查询条件
        Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(entry.getId()));
        ApLikesBehavior behavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        // 3. 如果是点赞操作  判断是否已经点过赞
        if (dto.getOperation()==0) {
            // 4. 没有点过赞则 像mongo点赞集合中 添加点赞数据
            if (behavior==null){
                behavior=new ApLikesBehavior();
                behavior.setEntryId(entry.getId());
                behavior.setOperation((short)0);
                behavior.setType((short)0);
                behavior.setArticleId(dto.getArticleId());
                behavior.setCreatedTime(new Date());
                mongoTemplate.save(behavior);
                return ResponseResult.okResult();
            }
        }
        // 5. 如果是取消点赞操作  在mongo点赞集合中 删除对应点赞数据
        if (dto.getOperation()==1){
            mongoTemplate.remove(behavior);
        }
        //*****添加文章点赞-发送消息******
        // =======================新加代码==========================
        NewBehaviorDTO mess = new NewBehaviorDTO();
        mess.setType(NewBehaviorDTO.BehaviorType.LIKES);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(dto.getOperation()==0?1:-1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(mess));
        log.info("点赞行为 发送消息 ==>{}",JSON.toJSONString(mess));
        // =======================新加代码==========================

        return ResponseResult.okResult();
    }
}
